﻿using System;
using System.Collections.Generic;
using System.IO; 
using System.Text; 

using MicroMq.Core.Models; 

namespace MicroMq.Core
{
    static class MqBuilder
    {
        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public static Stream Subscribe(string topic, string groupId)
        {
            var bytes = ByteWriteBuffer.Alloc()
                 .Write((byte)HandleType.Subscribe)
                 .Write($"{topic}\n{groupId}")
                 .ToBytes();
            return new MemoryStream(bytes);
        }
        /// <summary>
        /// 调度
        /// </summary>
        /// <param name="taskName"></param>
        /// <param name="cronExpression"></param>
        /// <returns></returns>
        public static Stream Dispatcher(string taskName, string cronExpression)
        {
            var bytes = ByteWriteBuffer.Alloc()
                 .Write((byte)HandleType.Dispatch)
                 .WriteUnknownString(taskName)
                 .WriteUnknownString(cronExpression)
                 .ToBytes();
            return new MemoryStream(bytes);
        }

        public static Stream Publish(string topicId, byte[] data)
        {
            var bytes = ByteWriteBuffer.Alloc()
                 .Write((byte)HandleType.Publish)
                 .WriteUnknownString(topicId)
                 .Write(data)
                 .ToBytes();
            return new MemoryStream(bytes);
        }

        public static Stream Publish(string topicId, int delay, byte[] data)
        {
            var bytes = ByteWriteBuffer.Alloc()
                 .Write((byte)HandleType.DelayPublish)
                 .WriteUnknownString(topicId)
                 .Write(delay)
                 .Write(data)
                 .ToBytes();
            return new MemoryStream(bytes);
        }
    }
}
